package com.hyy.libray.ui.activity;


import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hyy.libray.R;
import com.hyy.libray.contract.BaseLoadMoreContract;
import com.hyy.libray.presenter.BaseLoadMorePresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


/**
 * @ClassName ity
 * @Author hyy
 * @Description 列表样式基类
 * @Date 2019/1/8
 * @Time 10:44 AM
 */
public abstract class BaseLoadMoreActivity<T extends BaseLoadMorePresenter> extends BaseActivity<T> implements BaseLoadMoreContract.IView {

    private RecyclerView mRecyclerView;

    private SmartRefreshLayout smartRefreshLayout;


    public BaseQuickAdapter mCommonRecycleViewAdapter;

    public RecyclerView.LayoutManager mLayoutManager;


    @Override
    public void initView() {

        mRecyclerView = findViewById(R.id.recycle_view);

        smartRefreshLayout = findViewById(R.id.smart_refresh);

        if (mRecyclerView != null) {

            mLayoutManager = onLayoutManager();
            if (mLayoutManager == null) {

                mLayoutManager = new LinearLayoutManager(this);
            }

            mCommonRecycleViewAdapter = onCommonRecycleViewAdapter();

            if (mCommonRecycleViewAdapter == null) {

                throw new RuntimeException("onCommonRecycleViewAdapter返回不能为空");
            }


            mRecyclerView.setLayoutManager(mLayoutManager);

            mRecyclerView.setAdapter(mCommonRecycleViewAdapter);


        }

        if (smartRefreshLayout != null) {
            smartRefreshLayout.setEnableLoadMore(loadMoreEnable());
            smartRefreshLayout.setRefreshHeader(new ClassicsHeader(this));
            ClassicsFooter classicsFooter = new ClassicsFooter(this);
            classicsFooter.setNoMoreData(true);
            smartRefreshLayout.setRefreshFooter(classicsFooter);
            smartRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
                @Override
                public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                    mPresenter.getListDatas(false);
                }

                @Override
                public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                    mPresenter.getListDatas(true);
                }
            });
        }

    }

    /**
     * 布局ID
     *
     * @return
     */
    public abstract int onLayoutResId();


    @Override
    public int getLayoutResId() {

        if (onLayoutResId() == 0) {
            return R.layout.activity_base_loadmore;
        }
        return onLayoutResId();
    }

    /**
     * recyecleview，适配器
     *
     * @return
     */
    public abstract BaseQuickAdapter onCommonRecycleViewAdapter();

    /**
     * recyecleview，LayoutManager
     *
     * @return 默认LinearLayout.VERTICAL
     */
    public RecyclerView.LayoutManager onLayoutManager() {
        return new LinearLayoutManager(this);
    }

    @Override
    public void setListDatas(boolean isRefresh, boolean isLoadMore, List itemDatas) {
        if (isRefresh) {
            onFinishRefresh();
            if (itemDatas != null) {
                mCommonRecycleViewAdapter.setNewData(itemDatas);
            } else {
                mCommonRecycleViewAdapter.setNewData(new ArrayList());
            }
        } else {
            if (itemDatas != null) {
                mCommonRecycleViewAdapter.addData(itemDatas);
            }
        }

        if (isLoadMore) {
            onFinishLoadMore();
        } else {
            onStopLoadMore();
        }

    }

    /**
     * 暂定取消
     */
    public void onFinishRefresh() {

        if (smartRefreshLayout != null) {
            smartRefreshLayout.finishRefresh();
        }

    }

    /**
     * 暂定上拉加载更多
     */
    public void onFinishLoadMore() {
        if (smartRefreshLayout != null) {
            smartRefreshLayout.finishLoadMore();
        }

    }

    /**
     * 取消上拉加载更多
     */
    public void onStopLoadMore() {
        if (smartRefreshLayout != null) {
            smartRefreshLayout.finishLoadMoreWithNoMoreData();
        }
    }

    public boolean loadMoreEnable() {
        return true;
    }

}
